package com.zlf.mvc.interceptor;

import com.zlf.constant.CrowdConstant;
import com.zlf.entity.Admin;
import com.zlf.exception.AccessForbidenException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author ：张利锋
 * @date ：Created in 2020/11/13 16:20
 * @description： 拦截器查看是否登录
 * @version: 1.0
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取session对象
        HttpSession session = request.getSession();
        //2.获取admin
        Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        //3.判断是否有admin
        if (admin==null){
            //4.抛出异常
            throw new AccessForbidenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }
        //5.允许访问
        return true;
    }
}
